package xin.nick.demo.servlet;

import xin.nick.minicat.HttpProtocolUtil;
import xin.nick.minicat.HttpServlet;
import xin.nick.minicat.Request;
import xin.nick.minicat.Response;

import java.io.IOException;

/**
 * @author Nick
 * @date 2021/6/20
 * @description
 */
public class MyServlet extends HttpServlet {
    @Override
    public void doGet(Request request, Response response) {
        String content = "<h2>My get</h2>";
        String responseText = HttpProtocolUtil.getHttpHeader200(content.getBytes().length) + content;
        try {
            response.output(responseText);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doPost(Request request, Response response) {
        String content = "<h2>My post</h2>";
        String responseText = HttpProtocolUtil.getHttpHeader200(content.getBytes().length) + content;
        try {
            response.output(responseText);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void init() throws Exception {
        System.out.println("Servlet init");
    }

    public void destroy() throws Exception {
        System.out.println("Servlet destroy");
    }
}
